package com.wangsh.splider.orders.impl;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsh.splider.common.pojo.ApiResponse;
import com.wangsh.splider.common.pojo.ApiResponseEnum;
import com.wangsh.splider.common.pojo.JSONEnum;
import com.wangsh.splider.common.service.BaseServiceImpl;
import com.wangsh.splider.common.util.ConstatFinalUtil;
import com.wangsh.splider.common.util.PageInfoUtil;
import com.wangsh.splider.orders.dao.IAOrdersClientDao;
import com.wangsh.splider.orders.dao.IAOrdersUsersDao;
import com.wangsh.splider.orders.pojo.AOrdersClient;
import com.wangsh.splider.orders.pojo.AOrdersClientEnum;
import com.wangsh.splider.orders.pojo.AOrdersUsers;
import com.wangsh.splider.orders.service.IOrdersOperService;
import com.wangsh.splider.orders.service.IOrdersDbService;

/**
 * 订单Service的实现类
 * @author TeaBig
 */
@Service("ordersDbService")
public class OrdersDbServiceImpl extends BaseServiceImpl implements IOrdersDbService
{
	@Resource
	private IAOrdersUsersDao ordersUsersDao ;
	@Resource
	private IAOrdersClientDao ordersClientDao;
	@Resource
	private IOrdersOperService ordersOperService;
	
	/**
	 * ~cookieClient必须有值;
	 * ~如果输入email;会重新插入一条记录,email为cookie,
	 * ~后来为真正的email;并且cookieTarget要复制过来;
	 * ~email要么和cookie一样,要么是真实的email
	 * 
	 * email和cookie一样,如何处理
	 * email为真实的email如何处理
	 */
	@Override
	public ApiResponse<Object> saveOneClientService(AOrdersClient ordersClient)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		
		/* 如果Cookie重复的话,直接更新,而不是保存 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 根据Cookie查询记录 */
		condMap.clear();
		condMap.put("cookieClient", ordersClient.getCookieClient());
		ApiResponse<AOrdersClient> apiResponseRes = this.findOneClientService(condMap);
		AOrdersClient ordersClientRes = apiResponseRes.getDataOneJava();
		if(ordersClientRes != null)
		{
			apiResponse.setCode(ApiResponseEnum.INFO_RECORD_EXISTS.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
			
			/* 更新ip */
			ordersClientRes.setSysIpId(ordersClient.getSysIpId());
			/* 所有的请求头都要变 */
			ordersClientRes.setHttpSource(ordersClient.getHttpSource());
			ordersClientRes.setSysProxyId(0);
			/* 更新数据库 */
			/* 访问总次数+1 */
			ordersClientRes.setTotalNum(ordersClientRes.getTotalNum() + 1);
			ordersClientRes.setUpdateTime(new Date());
			this.updateOneClientService(ordersClientRes);
			return apiResponse ; 
		}
		/* 设置来源类型 */
		JSONObject sourceJSON = ordersClient.getHttpSourceJSON() ; 
		if(sourceJSON != null )
		{
			JSONObject requestJSON = (JSONObject) sourceJSON.get("request");
			String userAgent = requestJSON.get("user-agent") + "";
			String device = ConstatFinalUtil.device(userAgent);
			if(device.equalsIgnoreCase(ConstatFinalUtil.ANDROID))
			{
				ordersClient.setSouType(AOrdersClientEnum.SOUTYPE_ANDROID.getStatus());
			}else if(device.equalsIgnoreCase(ConstatFinalUtil.IPHONE))
			{
				ordersClient.setSouType(AOrdersClientEnum.SOUTYPE_IPHONE.getStatus());
			}else
			{
				ordersClient.setSouType(AOrdersClientEnum.SOUTYPE_PC.getStatus());
			}
		}
		
		/* 保存成功 */
		ordersClient.setTotalNum(1);
		ordersClient.setSuccedNum(1);
		
		ordersClient.setCreateTime(new Date());
		ordersClient.setUpdateTime(new Date());
		int res = this.ordersClientDao.save(ordersClient);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersClient.getId());
			//dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersClient);
			apiResponse.setDataOne(dataMap);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			
			/* 多线程访问一下目标网站的首页,就是为了获取Cookie */
//			Map<String, Object> paramsMap = new HashMap<String, Object>();
//			paramsMap.put("ordersOperService", ordersOperService);
//			paramsMap.put("ordersClient", ordersClient);
//			
//			ServiceCallableUtil serviceCallableUtil = new ServiceCallableUtil();
//			serviceCallableUtil.setParamsMap(paramsMap);
//			serviceCallableUtil.setOperType("findFaceBookCookie");
//			/* 多线程启动 */
//			ExecutorServiceUtil.submit(serviceCallableUtil);
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	public ApiResponse<Object> updateOneClientService(AOrdersClient ordersClient)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		
		ordersClient.setUpdateTime(new Date());
		int res = this.ordersClientDao.update(ordersClient);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersClient.getId());
			//dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersClient);
			apiResponse.setDataOne(dataMap);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<AOrdersClient> findCondListClientService(PageInfoUtil pageInfoUtil , Map<String, Object> condMap)
	{
		ApiResponse<AOrdersClient> apiResponse = new ApiResponse<AOrdersClient>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<AOrdersClient> ordersClientList = Collections.EMPTY_LIST;
		
		Map<String, List<AOrdersClient>> dataList = new HashMap<String, List<AOrdersClient>>();
		/* 对关键字进行模糊匹配 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%") ; 
		}
		
		/* 设置分页相关信息 */
		if(pageInfoUtil != null)
		{
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			ordersClientList = this.ordersClientDao.findList(condMap) ; 
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		}else
		{
			ordersClientList = this.ordersClientDao.findList(condMap) ; 
		}
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), ordersClientList);
		
		apiResponse.setDataListJava(ordersClientList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		return apiResponse; 
	}

	@Override
	public ApiResponse<AOrdersClient> findOneClientService(Map<String, Object> condMap)
	{
		ApiResponse<AOrdersClient> apiResponse = new ApiResponse<AOrdersClient>();
			
		Map<String, AOrdersClient> data = new HashMap<String, AOrdersClient>();
		AOrdersClient ordersClient = this.ordersClientDao.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), ordersClient);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(ordersClient);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		return apiResponse ; 
	}
	
	/**
	 * ~cookieClient必须有值;
	 * ~如果输入email;会重新插入一条记录,email为cookie,
	 * ~后来为真正的email;并且cookieTarget要复制过来;
	 * ~email要么和cookie一样,要么是真实的email
	 * 
	 * email和cookie一样,如何处理
	 * email为真实的email如何处理
	 */
	@Override
	public ApiResponse<Object> saveOneUsersService(AOrdersUsers ordersUsers)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		condMap.clear();
		condMap.put("email", ordersUsers.getEmail());
		ApiResponse<AOrdersUsers> apiResponseRes = this.findOneUsersService(condMap);
		AOrdersUsers ordersUsersRes = apiResponseRes.getDataOneJava() ;
		if(ordersUsersRes != null)
		{
			/* 说明记录已经存在,不应该再保存 */
			apiResponse.setCode(ApiResponseEnum.INFO_RECORD_EXISTS.getStatus());
			apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
			return apiResponse ; 
		}
		
		ordersUsers.setCreateTime(new Date());
		ordersUsers.setUpdateTime(new Date());
		int res = this.ordersUsersDao.save(ordersUsers);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersUsers.getId());
			//dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersUsers);
			apiResponse.setDataOne(dataMap);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
			
			/* 多线程访问一下目标网站的首页,就是为了获取Cookie */
//			Map<String, Object> paramsMap = new HashMap<String, Object>();
//			paramsMap.put("ordersOperService", ordersOperService);
//			paramsMap.put("ordersUsers", ordersUsers);
//			
//			ServiceCallableUtil serviceCallableUtil = new ServiceCallableUtil();
//			serviceCallableUtil.setParamsMap(paramsMap);
//			serviceCallableUtil.setOperType("findFaceBookCookie");
//			/* 多线程启动 */
//			ExecutorServiceUtil.submit(serviceCallableUtil);
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	public ApiResponse<Object> updateOneUsersService(AOrdersUsers ordersUsers)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		ordersUsers.setUpdateTime(new Date());
		int res = this.ordersUsersDao.update(ordersUsers);
		if (res > 0)
		{
			Map<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put(JSONEnum.EFFECT.getName(), res);
			dataMap.put(JSONEnum.ID.getName(), ordersUsers.getId());
			//dataMap.put(ApiResponseEnum.NAME_ONE.getName(), ordersUsers);
			apiResponse.setDataOne(dataMap);
			
			apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		} else
		{
			apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
		}
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
		return apiResponse;
	}
	
	@Override
	public ApiResponse<AOrdersUsers> findCondListUsersService(PageInfoUtil pageInfoUtil , Map<String, Object> condMap)
	{
		ApiResponse<AOrdersUsers> apiResponse = new ApiResponse<AOrdersUsers>();
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		List<AOrdersUsers> ordersUsersList = Collections.EMPTY_LIST;
		
		Map<String, List<AOrdersUsers>> dataList = new HashMap<String, List<AOrdersUsers>>();
		/* 对关键字进行模糊匹配 */
		if(condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			condMap.put("keyword", "%" + condMap.get("keyword") + "%") ; 
		}
		
		/* 设置分页相关信息 */
		if(pageInfoUtil != null)
		{
			/* 设置分页信息 */
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			ordersUsersList = this.ordersUsersDao.findList(condMap) ; 
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal() + ""));
			/* 向接口中放分页信息 */
			apiResponse.setPageInfoUtil(pageInfoUtil);
		}else
		{
			ordersUsersList = this.ordersUsersDao.findList(condMap) ; 
		}
		/* 将结果放到JSON中 */
		dataList.put(ApiResponseEnum.NAME_LIST.getName(), ordersUsersList);
		
		apiResponse.setDataListJava(ordersUsersList);
		apiResponse.setDataList(dataList);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		return apiResponse; 
	}

	@Override
	public ApiResponse<AOrdersUsers> findOneUsersService(Map<String, Object> condMap)
	{
		ApiResponse<AOrdersUsers> apiResponse = new ApiResponse<AOrdersUsers>();
			
		Map<String, AOrdersUsers> data = new HashMap<String, AOrdersUsers>();
		AOrdersUsers ordersUsers = this.ordersUsersDao.findOne(condMap);
		data.put(ApiResponseEnum.NAME_ONE.getName(), ordersUsers);
		apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
		/* 将结果放到JSON中 */
		apiResponse.setDataOne(data);
		apiResponse.setDataOneJava(ordersUsers);
		apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""),Collections.EMPTY_MAP);
		return apiResponse ; 
	}
}
